﻿/*************************************************************************
 *  Copyright (C), 2017-2018, YangTao tech. Co., Ltd.
 *  FileName: OrderMsgManage.cs
 *  Author: YangTao   Version: 1.0   Date: 2/9/2017
 *  Email:YangTao_Unity3D@163.com
 *  Version Description:
 *    用于命令类消息的生成管理。
 *  File Description:
 *    
 *  Class List:
 *    <ID>           <name>             <description>
 *     1.         OrderMsgManage         命令消息管理
 *  Function List:
 *    <ID>           <name>             <description>
 *     1.         OrderMsgHead           命令消息头
 *     2.         OrderMsgManage                     命令消息管理
 *  History:
 *    <ID>    <author>      <time>      <version>      <description>
 *     1.     YangTao     2/9/2017       1.0        Build this file.
 *************************************************************************/
using UnityEngine;
using System.Collections;
using System.Net;
using System.Collections.Generic;

/// <summary>头标记（区分命令类型）</summary>
public enum OrderHeadSign { ADDCLIENT, DELETCLIENT, DEFAULT }

/// <summary>命令消息头</summary>
public class OrderMsgHead
{
    /// <summary>命令消息头</summary>
    string m_OrderHeadSignStr;
    /// <summary>字符串类型ip端口集合</summary>
    string m_ipPorts;

    public OrderMsgHead(OrderHeadSign orderHeadSign, List<string> ipPortList)
    {
        m_OrderHeadSignStr = "";
        m_ipPorts = "";

        string hsStr = OrderHeadSignToHSStr(orderHeadSign);
        if (hsStr == null)
            return;

        if (ipPortList == null)
            return;

        m_OrderHeadSignStr = hsStr;

        foreach (string ipPort in ipPortList)
        {
            string str = ipPort.Replace(':', '_') + "_";
            m_ipPorts += str;
        }
    }

    /// <summary>转换OrderHeadSign成string</summary>
    public static string OrderHeadSignToHSStr(OrderHeadSign hs)
    {
        string temp = "";
        switch (hs)
        {
            case OrderHeadSign.ADDCLIENT:
                temp = "HS_ADDCLIENT_";
                break;
            case OrderHeadSign.DELETCLIENT:
                temp = "HS_DELETCLIENT_";
                break;          
            default:
                temp = null;
                break;
        }
        return temp;
    }

    /// <summary>转换OrderHeadSign成string</summary>
    public static OrderHeadSign HSStrToOrderHeadSign(string hs)
    {
        OrderHeadSign temp;
        switch (hs)
        {
            case "HS_ADDCLIENT_":
                temp = OrderHeadSign.ADDCLIENT;
                break;
            case "HS_DELETCLIENT_":
                temp = OrderHeadSign.DELETCLIENT;
                break;            
            default:
                temp = OrderHeadSign.DEFAULT;
                break;
        }
        return temp;
    }

    /// <summary>转换命令消息头</summary> 
    public string GetMsgHead()
    {
        return m_OrderHeadSignStr.Length.ToString() + "_" + m_ipPorts.Length.ToString() + "_" + m_OrderHeadSignStr + m_ipPorts;
    }
}

/// <summary>命令消息管理</summary> 
public class OrderMsgManage
{
    /// <summary>收到的命令消息头识别符</summary>
    string m_OrderHeadSignStr;
    /// <summary>收到的ipPort组</summary>
    string m_ipPorts;
    /// <summary>命令类型</summary>
    OrderHeadSign m_OrderHeadSign;
    /// <summary>存储ipPort的List</summary>
    List<string> m_ipPortList;

    public OrderMsgManage()
    {
        m_OrderHeadSignStr = "";
        m_ipPorts = "";
        m_OrderHeadSign = OrderHeadSign.DEFAULT;
        m_ipPortList = null;
    }

    /// <summary>创建命令消息</summary>  
    public string GetMsg(OrderHeadSign hs, List<string> ipPortList)
    {
        string headStr = CreateHead(hs, ipPortList);
        return headStr;
    }

    /// <summary>创建命令消息头</summary> 
    string CreateHead(OrderHeadSign hs, List<string> ipPortList)
    {
        OrderMsgHead mh = new OrderMsgHead(hs, ipPortList);
        return mh.GetMsgHead();
    }

    /// <summary>消息解析</summary> 
    public bool AnalysisMsg(string msg)
    {
        /// <summary>剩余字符串</summary> 
        string remainStr = msg;
        //"9_52_HS_TOALL_192.168.1.1_8080_192.168.1.2_8080_192.168.1.55_8080_I AM COME BACK!"

        int index = remainStr.IndexOf('_');//第一个'_'处的索引
        string hsLensStr = remainStr.Substring(0, index);//取出第一个'_'前字符
        remainStr = remainStr.Substring(index + 1);//去除'9_'后的字符

        //判定能否转换为数字
        int hsLens;
        if (!int.TryParse(hsLensStr, out hsLens))//返回hsLens=9
            return false;

        //剩余的字符串"51_HS_TOALL_192.168.1.1_8080_192.168.1.2_8080_192.168.1.55_8080_"
        index = remainStr.IndexOf('_'); //到第一个'_'的索引
        string ipPortLensStr = remainStr.Substring(0, index);//取出ip长度'52'
        remainStr = remainStr.Substring(index + 1);

        //剩余的字符串"HS_TOALL_192.168.1.1_8080_192.168.1.2_8080_192.168.1.55_8080_"
        int ipPortLens;
        if (!int.TryParse(ipPortLensStr, out ipPortLens))//返回ipPortLens=52
            return false;

        m_OrderHeadSignStr = remainStr.Substring(0, hsLens);//取出"HS_TOALL_"
        remainStr = remainStr.Substring(hsLens);
        m_OrderHeadSign = OrderMsgHead.HSStrToOrderHeadSign(m_OrderHeadSignStr);
        if (m_OrderHeadSign == OrderHeadSign.DEFAULT)
            return false;

        //剩余的字符串"192.168.1.1_8080_192.168.1.2_8080_192.168.1.55_8080_"
        m_ipPorts = remainStr.Substring(0, ipPortLens);//取出ip"192.168.1.1_8080_192.168.1.2_8080_192.168.1.55_8080_"     
        if(!AnalyseIPPort(m_ipPorts))
            return false;

        return true;
    }

    /// <summary>管理获得的IP字符串</summary>
    bool AnalyseIPPort(string ipPorts)
    {
        //正则表达式
        bool res = System.Text.RegularExpressions.Regex.IsMatch(ipPorts, @"^(([0-9]+[.]){3}[0-9]+[_][0-9]+[_])+$");
        if (!res)
            return res;

        string[] ipPortArray = ipPorts.Split('_');

        //清空前一次list
        if(m_ipPortList != null)
        {
            m_ipPortList.Clear();
            m_ipPortList = null;
        }

        m_ipPortList = new List<string>();
        for (int i = 0; i < ipPortArray.Length - 1;)
        {
            string ipPort = ipPortArray[i] + ":" + ipPortArray[i + 1];
            m_ipPortList.Add(ipPort);
            i += 2;
        }

        return true;
    }

    /// <summary>获得命令消息头标识</summary>
    public OrderHeadSign GetOrderHeadSign()
    {
        return m_OrderHeadSign;
    }

    /// <summary>获得客户端ip链表</summary>
    public List<string> GetIPPortList()
    {
        return m_ipPortList;
    }

}


